/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaCargoCadastro.java
 *
 * Created on 11/05/2012, 14:45:18
 */

package salutem.Telas;

import java.awt.Component;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import salutem.Beans.CargoBean;
import salutem.DAO.CargoDAO;
import salutem.Utils.Msg;
import salutem.Utils.Params;

/**
 *
 * @author Renato Doretto
 */
public class TelaCargoCadastro extends javax.swing.JDialog {

    private boolean inserir;
    private Integer idCargo;
    private CargoDAO daoCargo;
    private TelaCargo telaCargo;

    public TelaCargoCadastro(TelaCargo parent, boolean modal){
        super(parent, modal);
        initComponents();

        this.telaCargo = parent;
        this.daoCargo = new CargoDAO();
    }
    public TelaCargoCadastro(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lbDescricao = new javax.swing.JLabel();
        txDescricao = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btSalvar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbDescricao.setText("Descrição");

        txDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txDescricaoKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(lbDescricao))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbDescricao)
                .addGap(18, 18, 18)
                .addComponent(txDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(37, Short.MAX_VALUE))
        );

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/salutem/imagens/icone-salvar.png"))); // NOI18N
        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/salutem/imagens/icone-cancelar.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addComponent(btSalvar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btCancelar)
                .addContainerGap(55, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSalvar)
                    .addComponent(btCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        this.salvar();
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.cancelar();
    }//GEN-LAST:event_btCancelarActionPerformed

    private void txDescricaoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txDescricaoKeyTyped
        this.destacarCampo(this.txDescricao, false);
        if (evt.getKeyChar() == KeyEvent.VK_ENTER) {
            this.salvar();
        }
    }//GEN-LAST:event_txDescricaoKeyTyped

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TelaCargoCadastro dialog = new TelaCargoCadastro(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btSalvar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lbDescricao;
    private javax.swing.JTextField txDescricao;
    // End of variables declaration//GEN-END:variables

    private void cancelar() {
        this.setVisible(false);
        this.dispose();
    }

    protected boolean isInserir() {
        return inserir;
    }

    protected void setInserir(boolean inserir) {
        this.inserir = inserir;
        this.idCargo = null;
    }

    private void destacarCampo(Component c, boolean b){
        if(b){
            c.setBackground(Params.COR_CAMPO_VAZIO);
        }else{
            c.setBackground(Params.COR_CAMPO_NORMAL);
        }
    }

    private boolean verificarCampos(){
        boolean aux = false;
        String msg = "Preencha corretamente os campos. \n";
        if(this.txDescricao.getText().isEmpty()){
            aux = true;
            this.destacarCampo(this.txDescricao, aux);
        }
        if(aux){
            Msg.alerta(this, msg);
        }
        return aux;
    }

    protected void preencherCampos(int id){
        try{
            CargoBean cargo = daoCargo.getCargo(id);
            this.idCargo = cargo.getIdCargo();
            this.txDescricao.setText(cargo.getDescricao().trim().toUpperCase());
        }catch(SQLException ex){
            Msg.erro(this, "Erro ao preencher campos. \n"+ex.getMessage());
        }
    }
    
    private void salvar(){
        if(this.verificarCampos()){
            return;
        }
        try{
            if(this.isInserir()){
                CargoBean cargo = new CargoBean();
                cargo.setDescricao(this.txDescricao.getText().trim().toUpperCase());
                this.daoCargo.inserir(cargo);
                Msg.informacao(this, "Salvo com sucesso.");
                this.telaCargo.atualizarTabela();
                this.cancelar();
            }else{
                CargoBean cargo = new CargoBean();
                cargo.setIdCargo(this.idCargo);
                cargo.setDescricao(this.txDescricao.getText().trim().toUpperCase());
                this.daoCargo.alterar(cargo);
                Msg.informacao(this, "Alterado com sucesso.");
                this.telaCargo.atualizarTabela();
                this.cancelar();
            }
        }catch(SQLException ex){
            Msg.erro(this, "Erro ao salvar. \n"+ex.getMessage());
        }
    }
}
